package com.theanh.RssActivity.ui.model;

import com.theanh.RssActivity.R;
import com.theanh.RssActivity.Store;
import com.theanh.RssActivity.db.DbAdapter;
import com.theanh.RssActivity.entity.Item;
import com.theanh.RssActivity.util.DbUtils;
import com.theanh.RssActivity.util.Utils;

import android.database.SQLException;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ItemModel {
	private static final String TAG = "ItemModel";
	protected static final int BEGIN_UPDATE_IMAGE_MESSAGE = 0;
	protected static final int END_UPDATE_IMAGE_MESSAGE = 1;
	ImageView icon;
	TextView title;
	TextView publishDate;
	ImageButton button;
	TextView description;
	ImageView imgDescription;
	LinearLayout descriptionView;
	LinearLayout mainView;

	public Item item;

	public static ItemModel createModel(View view) {
		ItemModel model = new ItemModel();
		model.icon = (ImageView) view.findViewById(R.id.itemIconImageView);
		model.icon.setOnClickListener(model.expandOnClick);

		model.title = (TextView) view.findViewById(R.id.itemTitleTextView);
		model.title.setTypeface(Typeface.DEFAULT_BOLD);
		//model.title.setTextColor(android.graphics.Color.rgb(38, 157, 218));

		model.publishDate = (TextView) view
				.findViewById(R.id.itemPubDateTextView);
		// model.title.setTypeface(Typeface.DEFAULT_BOLD);
		model.publishDate.setTextColor(android.graphics.Color.GRAY);

		model.button = (ImageButton) view.findViewById(R.id.ImageButton01);
		model.button.setOnClickListener(model.readOnClick);
		// model.button.setImageResource(android.R.drawable.editbox_background_normal);

		model.description = (TextView) view
				.findViewById(R.id.itemDescriptionTextView);
		model.description.setTextColor(android.graphics.Color.BLACK);
		
		model.imgDescription = (ImageView) view
				.findViewById(R.id.imgDescription);
		model.imgDescription.setEnabled(Store.setting.isDisplayImage());
		
		model.descriptionView = (LinearLayout) view
				.findViewById(R.id.itemContentLinearLayout);
		model.descriptionView.setBackgroundResource(android.R.drawable.editbox_background);
		
		//model.descriptionView.setBackgroundResource(android.R.drawable.edit_text);
		model.mainView = (LinearLayout) view
				.findViewById(R.id.rowItemLinearLayout);
		model.mainView.setOnClickListener(model.openLinkOnClick);
		model.mainView.setBackgroundResource(R.xml.item_background);
		
//		model.mainView.setOnLongClickListener(model.openLinkOnClick);

		return model;
	}

	private OnClickListener expandOnClick = new OnClickListener() {
		public void onClick(View view) {
			if (descriptionView.getVisibility() == android.view.View.VISIBLE) {
				descriptionView.setVisibility(android.view.View.GONE);

				// Set icon minimize
				icon.setImageResource(R.drawable.expander_minimized);
			} else {
				descriptionView.setVisibility(android.view.View.VISIBLE);

				// Set icon maximize
				icon.setImageResource(R.drawable.expander_maximized);
				
				// Display image
				imgDescription.setImageBitmap(item.getImage());
			}
		}
	};

	private OnClickListener readOnClick = new OnClickListener() {
		public void onClick(View view) {
			if(descriptionView.getVisibility() == android.view.View.VISIBLE && !item.isReaded()){
				descriptionView.setVisibility(android.view.View.GONE);
				icon.setImageResource(R.drawable.expander_minimized);
			}
			
			item.setReaded(!item.isReaded());
			
			DbAdapter dbAdapter = null;
			try {;
				dbAdapter = DbUtils.createAndOpenDbAdapter();
				dbAdapter.setItemReaded(item);
			} catch (SQLException e) {
				Log.e(TAG, e.toString());
			} finally {
				DbUtils.closeDbAdapter(dbAdapter);
			}

			refreshModel();

			Store.channelModelSelected.updateNumberItem();
		}
	};

	private OnClickListener openLinkOnClick = new OnClickListener() {
		public void onClick(View view) {	
			Utils.showBrowserUrl(item.getUrl());
		}
	};
//	private OnLongClickListener openLinkOnClick = new OnLongClickListener() {
//		public boolean onLongClick(View view) {
//			if (item != null && item.getUrl() != null
//					|| item.getUrl().length() != 0) {
//				Log.i("onListItemClick", item.getUrl());
//			}
//			return true;
//		}
//	};

	public void bind(Item item) {
		this.item = item;
		title.setText(item.getTitle());
		publishDate.setText(item.getPublishDate().toLocaleString());
//		String url = Utils.paresImageUrlFromXml(item.getDescription());
//		if (url != null && url != "") {
//			Log.i(TAG, url);
//			imgDescription.setImageBitmap(Utils.loadImage(url));
//		} else {
//			imgDescription.setImageBitmap(null);
//		}
		
		if (Store.setting.isDisplayImage())
			imgDescription.setImageBitmap(item.getImage());

		description.setCompoundDrawablePadding(8);
		//description.setText(Utils.removeTagHtml(item.getDescription()));
		if(item.getDescription().length() == 0){
			description.setText("No description");
		}else{
			description.setText((item.getDescription()));
		}
		descriptionView.setVisibility(android.view.View.GONE);
		refreshModel();
	}

	private void refreshModel() {
		if (item.isReaded()) {
			// Set icon is unread
			button.setImageResource(R.drawable.read_off);
			int color = Store.context.getResources().getColor(R.color.color_title_item_readed);
			title.setTextColor(color);
		} else {
			// Set icon is readed
			button.setImageResource(R.drawable.read_on);
			
			int color = Store.context.getResources().getColor(R.color.color_title_item_unread);
			title.setTextColor(color);
		}
	}
	
	public void displayImageDescription(){
		sendMessage(END_UPDATE_IMAGE_MESSAGE);
	}
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case BEGIN_UPDATE_IMAGE_MESSAGE:
				// Do nothing
				Log.i(TAG, "BEGIN_UPDATE_IMAGE_MESSAGE");
				break;
			case END_UPDATE_IMAGE_MESSAGE:
				// Show image in description content
				if (ItemModel.this.item.getImage() != null && Store.setting.isDisplayImage()){
					ItemModel.this.imgDescription.setImageBitmap(ItemModel.this.item.getImage());
				}
				
				Log.i(TAG, "END_UPDATE_IMAGE_MESSAGE");
				break;
			}
			super.handleMessage(msg);
		}
	};
	
	protected void sendMessage(int identifier) {
		Message message = new Message();
		message.what = identifier;

		// send data to update GUI
		handler.sendMessage(message);
	}
}
